#ifndef DOCK_IR_PACKET_FINDER_HPP_
#define DOCK_IR_PACKET_FINDER_HPP_

/*****************************************************************************
 ** Includes
 *****************************************************************************/

#include <iomanip>
#include <StandardCplusplus.h>
#include <deque>

using namespace std;
/*****************************************************************************
 ** Namespaces
 *****************************************************************************/
/**
data struct
length : 5
startbit leftIR midIR rightIR checksum
0xAA     0x00   0x00  0x00    leftIR + midIR + rightIR

0x00 far
0x01 left near
0x02 min  near
0x03 right near 
0xff wuxiao

**/
class DockIRPacketFinderBase
{
public:
  typedef deque<unsigned char> BufferType;

  enum packetFinderState
  {
    clearBuffer = 0,
    waitingForStx,
    waitingForPayloadSize,
  };
  enum packetFinderState state;

  unsigned int size_stx;
  //unsigned int size_etx;
  unsigned int size_length_field;  
protected:


  bool variable_size_payload;
  unsigned int size_max;
  unsigned int size_payload;
  unsigned int size_checksum_field;

  BufferType STX;
  BufferType ETX;
  BufferType buffer;

  bool verbose;

public:
  DockIRPacketFinderBase(); /**< Default constructor. Use with configure(). **/

  virtual ~DockIRPacketFinderBase() {};

  void configure();
  void clear();
  void enableVerbose();
  virtual bool update(const unsigned char * incoming, unsigned int numberOfIncoming);
  virtual bool checkSum();
  unsigned int numberOfDataToRead();
  void getBuffer(BufferType & bufferRef);
  void getPayload(BufferType & bufferRef);

protected:
  bool WaitForStx(const unsigned char datum);
  bool waitForPayloadSize(const unsigned char * incoming, unsigned int numberOfIncoming, bool & foundPacket);  
};

#endif /* DOCK_IR_PACKET_FINDER_HPP_ */
